package com.example.controller;

import com.example.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseController {

    @Autowired
    JwtUtils jwtUtils;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected int userid;
    protected String username;
    protected String password;

    @ModelAttribute
    public void setResAnReq(HttpServletRequest request,HttpServletResponse response) throws Exception{
        this.request=request;
        this.response=response;
        String token = request.getHeader("Authorization");
        System.out.println(token);
        if(token==null||"".equals(token)||"null".equals(token)){
            return;
        }
        Claims claims = jwtUtils.parseJwt(token);
        if(claims==null){
            return;
        }
       this.userid=(int)claims.get("userid");
        this.username=(String)claims.get("username");
        this.password=(String)claims.get("password");
    }

}
